package com.isoft.mapper;

import com.isoft.entity.BaseCfueEntity;
import org.apache.ibatis.annotations.*;

@Mapper
public interface BaseCfueMapper {

    @Insert("INSERT INTO MSG_META(SENDER, RECEIVER, SEQUENCE_NUMBER, DATE_TIME, MSG_TYPE, SUB_TYPE) " +
            "VALUES(" +
            "#{meta.sender,jdbcType=VARCHAR}, " +
            "#{meta.receiver,jdbcType=VARCHAR}, " +
            "#{meta.sequenceNumber,jdbcType=NUMERIC}, " +
            "TO_TIMESTAMP(#{meta.dateTime,jdbcType=VARCHAR}, 'yyyymmddhh24miss'), " +
            "#{meta.type,jdbcType=VARCHAR}, " +
            "#{meta.subType,jdbcType=VARCHAR})")
    @Options(useGeneratedKeys = true, keyProperty = "meta.msgId", keyColumn = "MSG_ID")
    void insertMeta(BaseCfueEntity entity);

    @Select("SELECT IFNULL(MAX(FLIGHT_ID), 0) + 1 FROM FLIGHT_BASE")
    Long getNextFlightId();

    @Insert("INSERT INTO FLIGHT_BASE(FLIGHT_ID, FLIGHT_ATTRIBUTE, STATUS) " +
            "VALUES(#{flightId}, 'VIRTUAL', 'NORMAL')")
    void insertDummyFlight(@Param("flightId") Long flightId);

    @Insert("INSERT INTO AIRCRAFT(FLIGHT_ID, AIRCRAFT_TYPE, AIRCRAFT_NUMBER) " +
            "VALUES(" +
            "#{flightId,jdbcType=NUMERIC}, " +
            "#{aircraftType,jdbcType=VARCHAR}, " +
            "#{aircraftNumber,jdbcType=VARCHAR})")
    void insertAircraftWithFlight(@Param("flightId") Long flightId,
                                  @Param("aircraftType") String aircraftType,
                                  @Param("aircraftNumber") String aircraftNumber);

    @Insert("INSERT INTO FLIGHT_IRREGULARITY(" +
            "FLIGHT_ID, ABNORMAL_STATUS, ABNORMAL_REASON, " +
            "INTERNAL_STATUS, INTERNAL_REASON, MANUAL_ABNORMAL_STATUS, MANUAL_ABNORMAL_REASON, " +
            "MANUAL_INTERNAL_STATUS, MANUAL_INTERNAL_REASON) " +
            "VALUES(" +
            "#{flightId,jdbcType=NUMERIC}, " +
            "'CFUE', " +
            "NULL, " +
            "'CFUE', " +
            "NULL, " +
            "NULL, NULL, NULL, NULL)")
    int insertIrregularity(@Param("flightId") Long flightId);
}